import { create, all } from 'mathjs'
import fileSaver from 'file-saver'
import numeral from 'numeral'

let math = create(all, {
  number: 'BigNumber',
  precision: 64
})

export default {
  data() {
    return {
      tableKey: '1',
      allColumnSelected: true,
      allColumnSelectedIndeterminate: false,
      tableColumnList: []
    }
  },
  methods: {
    formatNumberTiny({ formula, format }) { // 格式化数字
      let result = math.format(math.evaluate(formula))
      return numeral(result).format(format)
    },
    formatNumber({ formula, format = '0.00' }) { // 格式化数字
      let result = math.format(math.evaluate(formula))
      return numeral(result).format(format)
    },
    async downloadFile(response) { // 导出－附件
      let result = response.data
      if (result.code && +result.code !== 200) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: result.msg || '操作失败！'
        })
      } else {
        let blob = new Blob([result]) // 构造一个blob对象来处理数据
        // console.log(blob)
        let fileName = response.headers['content-disposition'] && response.headers['content-disposition'].split(';')[1].split('=')[1] // 获取文件名
        // let fileName = `${uuid(15, 10)}`
        // let fileName = `${data.name}`
        fileName = decodeURI(fileName)
        // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
        // IE10以上支持blob但是依然不支持download
        /* if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
          const link = document.createElement('a') // 创建a标签
          link.download = fileName // a标签添加属性
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          document.body.appendChild(link)
          link.click() // 执行下载
          URL.revokeObjectURL(link.href) // 释放url
          document.body.removeChild(link) // 释放标签
        } else { // 其他浏览器
          navigator.msSaveBlob(blob, fileName)
        } */
        fileSaver.saveAs(blob, fileName)
      }
    },
    handleCheckedAllTableColumn(val) { // 表头全部选择
      if (val === false) {
        this.allColumnSelected = true
        return
      }
      for (const key in this.tableColumnList) {
        this.tableColumnList[key].visible = val
      }
      this.allColumnSelected = val
      this.allColumnSelectedIndeterminate = false
    },
    handleCheckedTableColumn(item) {
      let totalCount = 0
      let selectedCount = 0
      for (const key in this.tableColumnList) {
        ++totalCount
        selectedCount += this.tableColumnList[key].visible ? 1 : 0
      }
      if (selectedCount === 0) {
        this.$notify.error({
          title: '错误',
          message: '请至少选择一列！'
        })
        this.$nextTick(function() {
          item.visible = true
        })
        return
      }
      this.allColumnSelected = selectedCount === totalCount
      this.allColumnSelectedIndeterminate = selectedCount !== totalCount && selectedCount !== 0
    },
    getTableColumns() {
      const columns = {}
      this.$refs.table.columns.forEach(e => {
        if (!e.property || !e.label || e.type !== 'default') {
          return
        }
        columns[e.property] = {
          label: e.label,
          visible: true
        }
      })
      this.tableColumnList = columns
    },
    columnsVisible(col) {
      let { tableColumnList } = this
      return !tableColumnList || !tableColumnList[col] ? true : tableColumnList[col].visible
    },
    //处理时间区间
    processDateRange(prop,obj){
      if(obj[prop].length == 2){
        //obj[prop] = obj[prop][0] + '-' + obj[prop][1];
        obj.beginTime = obj[prop][0]
        obj.endTime = obj[prop][1]
        delete obj[prop] 
      }
      return obj;
    }
  },
  watch: {
    tableColumnList: {
      handler: function(newVal, oldVal) {
        this.tableKey = (+this.tableKey) + 1
      },
      deep: true
    }
  }
}
